import axios from 'axios';

import $message from '@/common/message.js';
// import store from '@/store/store';
import router from '../router/index.js';
// import md5 from 'js-md5';
import cookies from '../common/cookies-util.js';

let url = process.env.VUE_APP_BASEURL;

if (process.env.NODE_ENV === 'development') {
  /* 开发环境 */
  url = process.env.VUE_APP_BASEURL;
} else {
  /* 生产环境 */
  url = window.webConfig.webApiBaseUrl;
}

var get = function (apibase, data) {
  var that = this;
  var date = Date.parse(new Date());
  var token = JSON.parse(localStorage.getItem('nbcToken'));
  var functionName = localStorage.getItem('functionName');

  return new Promise((resolve, reject) => {
    axios({
      headers: {
        'Content-Type': 'application/json',
        Authorization: token,
        BrowserType: 'WEB',
        FunctionName: Base64.encode(functionName),
      },
      method: 'GET',
      url: url + apibase,
      // params: {
      // 	id:data
      // },
    })
      .then(function (response) {
        switch (response.status) {
          case 201:
            resolve(response.data);
            break;
          case 0:
            resolve(response.data);
            // store.commit("setToken", null);
            // store.commit("setLoginStatus", false);
            // cookies.setItem("isLogin", false);
            // router.push("/login");
            // $message.error(response.data.message);
            break;
          case 200:
            if (response.data && response.data.Status === 403) {
              localStorage.removeItem('nbcToken');
              localStorage.removeItem('nbcUserInfo');
             // localStorage.removeItem('nbcUsername');
              localStorage.removeItem('nbcPassword');
              window.location.href = '#/login';
              $message.error(response.data.Message);
              break;
            }
            resolve(response.data);
            break;
          default:
            // $message.error(response.data.desc);
            resolve(response.data);
          // reject(response.data);
        }
      })
      .catch(err => {
        $message.error(`服务器异常:${err}`);
        reject(err);
      });
  });
};
var post = function (apibase, data) {
  var that = this;
  var date = Date.parse(new Date());
  var token = JSON.parse(localStorage.getItem('nbcToken'));
  var functionName = localStorage.getItem('functionName');

  return new Promise((resolve, reject) => {
    axios({
      headers: {
        'Content-Type': 'application/json',
        Authorization: token,
        BrowserType: 'WEB',
        FunctionName: Base64.encode(functionName),
      },
      method: 'POST',
      data: JSON.stringify(data),
      url: url + apibase,
    })
      .then(function (response) {
        switch (response.status) {
          case 200:
            if (response.data.Status == 0) {
              $message.error(response.data.Message);

              resolve(response.data);
            } else if (response.data && response.data.Status === 403) {
              localStorage.removeItem('nbcToken');
              localStorage.removeItem('nbcUserInfo');
             // localStorage.removeItem('nbcUsername');
              localStorage.removeItem('nbcPassword');
              window.location.href = '#/login';
              $message.error(response.data.Message);
              break;
            } else {
              resolve(response.data);
            }
            break;
          case 0:
            $message.error(response.Message);
            break;
          case 10000:
            resolve(response.data);
            break;
          default:
            resolve(response.data);
            $message.error(response.Message);
            reject(response.data);
        }
      })
      .catch(err => {
        $message.error(`服务器异常:${err}`);
        reject(err);
      });
  });
};

var Filepost = function (apibase, data) {
  var that = this;
  var date = Date.parse(new Date());
  var token = JSON.parse(localStorage.getItem('nbcToken'));
  var functionName = localStorage.getItem('functionName');
  return new Promise((resolve, reject) => {
    axios({
      headers: {
        'Content-Type': 'application/json;charset=utf-8',
        Authorization: token,
        BrowserType: 'WEB',
        FunctionName: Base64.encode(functionName),
      },
      method: 'POST',
      data: data,
      url: url + apibase,
    })
      .then(function (response) {
        switch (response.data.code) {
          case 201:
            resolve(response.data);
            break;
          case 0:
            resolve(response.data);
            // store.commit("setToken", null);
            // store.commit("setLoginStatus", false);
            // cookies.setItem("isLogin", false);
            // router.push("/login");
            // $message.error(response.data.desc);
            break;
          case 200:
            resolve(response.data);
            break;
          case 200:
            resolve(response.data);
            break;
          default:
            resolve(response.data);
          // $message.error(response.data.desc);
          // reject(response.data);
        }
      })
      .catch(err => {
        // $message.error(`服务器异常:${err}`);
        // console.log(err);
        // reject(err);
      });
  });
};

var put = function (apibase, data) {
  var that = this;
  var date = Date.parse(new Date());
  var token = JSON.parse(localStorage.getItem('nbcToken'));
  var functionName = localStorage.getItem('functionName');
  return new Promise((resolve, reject) => {
    axios({
      headers: {
        'Content-Type': 'application/json',
        Authorization: token,
        BrowserType: 'WEB',
        FunctionName: Base64.encode(functionName),
      },
      method: 'PUT',
      data: JSON.stringify(data),
      url: url + apibase,
    })
      .then(function (response) {
        switch (response.data.code) {
          case 201:
            $message.success(response.data.Message);
            resolve(response.data.data);
            break;
          case 0:
            store.commit('setToken', null);
            store.commit('setLoginStatus', false);
            cookies.setItem('isLogin', false);
            router.push('/login');
            $message.error(response.data.Message);
            break;
          case 200:
            resolve(response.data.data);
            break;
          case 0:
            store.commit('setToken', null);
            store.commit('setLoginStatus', false);
            cookies.setItem('isLogin', false);
            router.push('/login');
            $message.error(response.data.Message);
            break;
          default:
            $message.error(response.data.Message);
            reject(response.data.Message);
        }
      })
      .catch(err => {
        $message.error(`服务器异常:${err}`);
        console.log(err);
        reject(err);
      });
  });
};
var remove = function (apibase, data) {
  var that = this;
  var date = Date.parse(new Date());
  var token = JSON.parse(localStorage.getItem('nbcToken'));
  var functionName = localStorage.getItem('functionName');

  return new Promise((resolve, reject) => {
    axios({
      headers: {
        'Content-Type': 'application/json',
        Authorization: token,
        BrowserType: 'WEB',
        FunctionName: Base64.encode(functionName),
      },
      method: 'DELETE',
      data: JSON.stringify(data),
      url: url + apibase,
    })
      .then(function (response) {
        switch (response.data.code) {
          case 201:
            $message.success(response.data.Message);
            resolve(response.data.data);
            break;
          case 0:
            store.commit('setToken', null);
            store.commit('setLoginStatus', false);
            cookies.setItem('isLogin', false);
            router.push('/login');
            $message.error(response.data.Message);
            break;
          case 200:
            resolve(response.data.data);
            break;
          default:
            $message.error(response.data.message);
            reject(response.data.message);
        }
      })
      .catch(err => {
        $message.error(`服务器异常:${err}`);
        console.log(err);
        reject(err);
      });
  });
};
export default {
  get,
  post,
  put,
  remove,
  Filepost,
};
